﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace PO_2011.Filter
{
    public partial class BinarizationForm : Form
    {
        private PictureBox imageBox;
        private Bitmap initImage;
        bool savedChanges;

        public BinarizationForm(PictureBox sourceImageBox)
        {
            InitializeComponent();

            imageBox = sourceImageBox;
            initImage = new Bitmap(sourceImageBox.Image);
            savedChanges = true;
        }

        private void binarizationTrackBar_ValueChanged(object sender, EventArgs e)
        {
            imageBox.Image = Binarization.GlobalBinarization(initImage, binarizationTrackBar.Value);
            savedChanges = false;
        }

        private void binarizationTrackBar_MouseDown(object sender, MouseEventArgs e)
        {
            ImageHistory.History.addStep(new Bitmap(imageBox.Image));
        }

        private void otsuButton_Click(object sender, EventArgs e)
        {
            binarizationTrackBar.Value = Binarization.OtsuThresholding(initImage);
            binarizationTrackBar_ValueChanged(null, null);
            savedChanges = false;
        }

        private void whiteRohrerButton_Click(object sender, EventArgs e)
        {
            ImageHistory.History.addStep(new Bitmap(imageBox.Image));
            
            int windowSize;
            float param;
            bool isInt = int.TryParse(windowsSizeBox.Text.Trim(), out windowSize);
            bool isFloat = float.TryParse(paramBox.Text.Trim(), out param);
            if (!isInt) MessageBox.Show("Nieprawidłowa wielkość okna.");
            else if (!isFloat) MessageBox.Show("Nieprawidłowy parametr.");
            else imageBox.Image = Binarization.WhiteRohrerThresholding(initImage, windowSize, param);
            savedChanges = false;
        }

        private void bernsenButton_Click(object sender, EventArgs e)
        {
            ImageHistory.History.addStep(new Bitmap(imageBox.Image));
            
            int windowSize;
            float param;
            bool isInt = int.TryParse(windowsSizeBox.Text.Trim(), out windowSize);
            bool isFloat = float.TryParse(paramBox.Text.Trim(), out param);
            if (!isInt) MessageBox.Show("Nieprawidłowa wielkość okna.");
            else if (!isFloat) MessageBox.Show("Nieprawidłowy parametr.");
            else imageBox.Image = Binarization.BernsenThresholding(initImage, windowSize, param);
            savedChanges = false;
        }

        private void niblackButton_Click(object sender, EventArgs e)
        {
            ImageHistory.History.addStep(new Bitmap(imageBox.Image));

            int windowSize;
            float param;
            bool isInt = int.TryParse(windowsSizeBox.Text.Trim(), out windowSize);
            bool isFloat = float.TryParse(paramBox.Text.Trim(), out param);
            if (!isInt) MessageBox.Show("Nieprawidłowa wielkość okna.");
            else if (!isFloat) MessageBox.Show("Nieprawidłowy parametr.");
            else imageBox.Image = Binarization.NilbackThresholding(initImage, windowSize, param);
            savedChanges = false;
        }

        private void binarResetButton_Click(object sender, EventArgs e)
        {
            ImageHistory.History.addStep(new Bitmap(imageBox.Image));
            
            binarizationTrackBar.Value = 128;
            imageBox.Image = initImage;
        }

        private void binarSaveButton_Click(object sender, EventArgs e)
        {
            initImage = new Bitmap(imageBox.Image);
            savedChanges = true;
        }

        private void BinarizationForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!savedChanges) imageBox.Image = initImage;
        }

    }
}
